package org.xinghai.sql.main.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.xinghai.sql.common.api.Result;
import org.xinghai.sql.main.dto.CreateTableRelationshipDto;
import org.xinghai.sql.main.generator.domain.TableRelationships;
import org.xinghai.sql.main.generator.service.TableRelationshipsService;

/**
 * 表与表之间的关联信息的存储
 * @author lijia
 * &#064;date  2025/07/28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/relationships")
public class TableRelationshipsController {

    private final TableRelationshipsService relationshipsService;
    // 创建新的表关联关系
    @PostMapping("/create")
    public Result<String> createTableRelationship(
            @RequestBody CreateTableRelationshipDto dto) {
        TableRelationships entity = new TableRelationships();
        BeanUtils.copyProperties(dto, entity);
        relationshipsService.save(entity);
        return Result.success("关联关系创建成功");
    }

    // 补充其他基础CRUD接口
    @DeleteMapping("/{id}")
    public Result<String> deleteRelationship(@PathVariable Integer id) {
        relationshipsService.removeById(id);
        return Result.success("关联关系已删除");
    }
    //获取表与表之间的关联信息
    @GetMapping("/{id}")
    public Result<TableRelationships> getRelationship(@PathVariable Integer id) {
        return Result.success(relationshipsService.getById(id));
    }
}
